#include "my_common.h"
#include "Global.h"
#include <stdlib.h>
#include "string.h"

void Str2Hex(char *pStr,unsigned char* des)
{
	unsigned long i=0;
	while(pStr[i]!=0)
	{
		if(pStr[i] >= '0' && pStr[i] <= '9')
		{
			des[i]=pStr[i]-'0';
		}
		i++;
	}
}
void Mem2Mem(unsigned char *Src,unsigned char* Des,int size)
{
	unsigned long i=0;
	while(i<size)
	{
		Des[i]=Src[i];
		i++;
	}
}
unsigned char MemCmp(unsigned char *Src,unsigned char* Des,int size)
{
	unsigned long i=0;
	unsigned char result=0;
	while(i<size)
	{
		if(Des[i]==Src[i])
			i++;
		else
			break;
	}
	if(i<size)
		result=1;
	return result;
}

unsigned char MemCompare(unsigned char *Src,unsigned char* Des,int size) //if compare equal ,return 1
{
	int i=0;
	unsigned char result=0;
	while(i<size)
	{
		if(Des[i]==Src[i])
			i++;
		else
			break;
	}
	if(i>=size)
		result=1;
	return result;
}

int StrLen(STR_T pStr)
{
  int Len = 0;
  while(pStr[Len] != 0)
  {
    Len++;
    if(Len > STR_MAX_LEN)
    {
      break;
    }
  }
  return Len;
}

/*********************************************************************************************************//**
  * @brief  MemCpy.
  * @param  TBD
  * @retval None
  ***********************************************************************************************************/
unsigned char MemCpy(void* pDes, void* pSrc, unsigned short size)//0x887c-0x58=0x8824
{
  unsigned char* des = (unsigned char*)pDes;
  unsigned char* src = (unsigned char*)pSrc;
  
  while(size--)
  {
    *des = *src;
    des++;
    src++;
  }
  return MY_TRUE;
}

void My_MemCpy(unsigned char *psrc,unsigned char *pdes,unsigned char len)
{
	unsigned char i=0;
	for(i=0;i<len;i++)
		pdes[i] = psrc[i];
}

unsigned char HexAscii2Hex(unsigned char *pStr, unsigned char size)
{
  unsigned char val = 0;
  unsigned char i;    
  for(i=0;i<size;i++)
  {
    char c = pStr[i];
    if(c >= 'A')
    {
      if(c >= 'a')
      {
        val |= (unsigned char)((pStr[i] - 'a') + 10) << ((size-i-1)*4);
      }
      else
      {
        val |= (unsigned char)((pStr[i] - 'A') + 10) << ((size-i-1)*4);
      }
    }
    else
    {
      val |= (unsigned char)(pStr[i] - '0') << ((size-i-1)*4);
    }
  }

  return val;
}

unsigned char hex_2_ascii(unsigned char src)
{
  unsigned char result=0;
  if((src >= 0) && (src <= 9))
    result = src - 0 + '0';
  else if((src >= 10) && (src <= 15))
    result = src - 10 + 'A';
  return result;
}


void Hex_2_HexAscii(unsigned char src,unsigned char *pdes)
{
  *pdes = hex_2_ascii((src & 0xF0) >> 4);
  *(pdes+1) = hex_2_ascii(src & 0x0F);
}

unsigned char strtok_ex(char *p,char *des,unsigned char  index,char *psign)
{
	unsigned char  i=0;
	char *p_=NULL;
	char *ptmp=NULL;
	char table[84]={0};
	strcpy(table,p);
	p_ = table;
	ptmp = strtok(p_,psign);
	while(ptmp != NULL)
	{		
		if(i == index)
		{
			strcpy(des,ptmp);
			ptmp = NULL;
			return 1;
		}
		ptmp = strtok(NULL,psign);
			
		++i;
	}
	ptmp = NULL;
		return 0;
}

void str_16H_2_hex(char *psrc,char *pdes,unsigned char len)
{
	unsigned char i=0;
	for(i=0;i<len;i++)
	{
		if((psrc[i] >= '0') && (psrc[i] <= '9'))
			pdes[i] = psrc[i] - 0x30;
		else if((psrc[i] >= 'a') && (psrc[i] <= 'f'))
			pdes[i] = psrc[i] - 0x57;
		else if((psrc[i] >= 'A') && (psrc[i] <= 'F'))
			pdes[i] = psrc[i] - 0x37;;	
	}
}


